<?php
/*
* 采集千团网中团购商品信息
* lsj 2010-8-26
*/
set_time_limit(0);
require_once '../inc.php';

#源地址
$urls = array(
'http://www.1000tuan.com/quanguo/',
'http://www.1000tuan.com/',
'http://www.1000tuan.com/guangzhou/',
'http://www.1000tuan.com/shenzhen/',
);

$i = 0;
$hp = new http();
foreach($urls as $city => $url){
	$res = file_get_contents($url);
	if($res){
		$res = str_replace(array("\r", "\n", "\t", ' '), '', $res);
		preg_match_all('|<liclass="dealj-item"data-sort="(.*)"data-category="(.*)"title="(.*)"><divclass="dealInt">|U', $res, $g0);
		preg_match_all('|>【(.*)】</a><aclass="title"href="([^>]*)target="_blank">([^<]*)</a>|U', $res, $g1);
		preg_match_all('|><imgsrc="(.*)"alt="(.*)"data-scover="|U', $res, $g2);
		preg_match_all('|<spanclass="timeleftj-counter"data-start="(.*)"data-end="(.*)"><emclass="dayj-day">|U', $res, $g3);
        preg_match_all('|<spanclass="district">(.*)</span><spanclass="shadow"><b>|U', $res, $g4);
        preg_match_all('|<divclass="price"><spanclass="worth">原价：￥<spanstyle="text-decoration:line-through;">(.*)</span>|U', $res, $g5);
 
        //print_r($g1);exit;
        if($g0){
        
            $data = array();
            
            foreach($g0[1] as $k=>$g){
                if(!$g0[2][$k]) $g0[2][$k] = 'other';
                $cate = array('relaxation'=>1, 'food'=>2, 'fashion'=>3, 'hairdressing'=>4, 'other'=>8);
                $data['cate_id'] = $cate[$g0[2][$k]];
                $data['title'] = $g0[3][$k];
                $data['nowprice'] = $g;
                $data['website_id'] = $g1[1][$k];
                $weburl = parse_str(current(explode('"', $g1[2][$k])));
                $weburl = 'http://'.$weburl['host'];
                $data['url'] = $weburl;
                $data['stitle'] = $g1[3][$k];
                $img = $g2[1][$k];
                $data['fromtime'] = $g3[1][$k]; 
                $data['totime'] = $g3[2][$k]; 
                $data['site'] = $g4[1][$k];
                $data['firstprice'] = $g5[1][$k];
                $data['city_id'] = $city;
                $data['createtime'] = time();
                
                $wid = $db->select_var("select id from x_website where webname='{$data['website_id']}' limit 1");
                if(!$wid){
                   $wid = $db->insert("insert into x_website(webname,weburl) values('{$data['website_id']}','{$weburl}')"); 
                }
                $data['website_id'] = $wid;
                
                print_r($data);exit;
                if($gid = $db->insert($data, 'x_goods')){
                    $img = file_get_contents($img);
                    //$hp->request($img);
                    //$img = $hp->getResponseBody();
                    if($img){
                        $imgPath = dirname(dirname(__FILE__)).'/images/team/'.ceil($gid / 600);
                        //echo $imgPath;
                        //echo $img;
                        if(!is_dir($imgPath)) mkdir($imgPath, 0775);
                        $imgPath .= '/'.$gid.'.gif'; 
                        file_put_contents($imgPath, $img);
                    }
                    $i++;
                    //exit;
                }
                
            }
        }
        



		
	}
}

echo $i;









?>

<!--<li class="deal j-item" data-sort="5" data-category="food" title="今日优惠：仅5元！体验韩中阁三千里烤肉城价值38元好评热菜：铁板五花肉"> 
<div class="dealInt"> 
	<h3> 
		<a class="source" href="http://yiqi001.com/" onmousedown="qt.cc({'Sa[sa_key]':'index_deals_source','Sa[site_key]':'http://yiqi001.com/'});" target="_blank" title="一起网">【一起网】</a> 
		<a class="title" href="http://yiqi001.com/" onmousedown="qt.cc({'Sa[sa_key]':'index_deals_title','Sa[site_key]':'http://yiqi001.com/'});" target="_blank">韩中阁三千里烤肉城好评热菜：铁板五花肉</a> 
	</h3>
	<div class="info"> 
		<p class="cover"><a href="http://yiqi001.com/" onmousedown="qt.cc({'Sa[sa_key]':'index_deals_cover','Sa[site_key]':'http://yiqi001.com/'});" target="_blank"><img src="http://yiqi001.com/static/team/2010/0820/12823113037838.jpg" alt="今日优惠：仅5元！体验韩中阁三千里烤肉城价值38元好评热菜：铁板五花肉" data-scover="http://yc.1000tuan.com/api/track/?p=http://yiqi001.com/static/team/2010/0820/12823113037838.jpg" onerror="switchCover(this);"/></a></p> 
		<span class="timeleft j-counter" data-start="1282723200" data-end="1282809540"><em class="day j-day">?</em>天<em class="hour j-hour">?</em>小时<em class="minute j-minute">?</em>分<em class="second j-second">?</em>秒</span> 
		<span class="district">东城</span> 
		<span class="shadow"><b><i></i></b></span>
	</div>
	<div class="price"> 
		<span class="worth">原价：￥<span style="text-decoration:line-through;">38</span></span> 
		<span class="discount">1.3折</span> 
		<strong class="amount">￥5</strong> 
	</div> 
	<div class="buy"> 
		<a href="http://yiqi001.com/" onmousedown="qt.cc({'Sa[sa_key]':'index_deals_buy','Sa[site_key]':'http://yiqi001.com/'});" target="_blank" title="立即购买">购买 &raquo;</a> 
	</div> 
</div> 
</li>-->